package com.java.huxb.backend.film.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.huxb.backend.film.controller.vo.ActorResponseVo;
import com.java.huxb.backend.film.controller.vo.DescribeFilmsRespVO;
import com.java.huxb.backend.film.controller.vo.FilmSavedReqVO;
import com.java.huxb.backend.film.service.FilmActorsApi;
import com.java.huxb.backend.film.service.FilmServiceApi;
import com.java.huxb.backend.utils.exception.CommonServiceException;
import com.java.huxb.backend.utils.vo.BasePageVO;
import com.java.huxb.backend.utils.vo.BaseResponseVO;
import com.java.huxn.backend.api.film.vo.DescribeFilmRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huxb
 * @date 2020/1/29 0029 下午 1:39
 *
 */
@RestController
@RequestMapping("/films")
public class FilmController {
    
    @Autowired
    private FilmActorsApi filmActorsApi;

    @Autowired
    private FilmServiceApi filmServiceApi;
    
    /**
     * 分页获取演员列表
     * @param basePageVO
     * @return
     */
    @RequestMapping("/actors")
    public BaseResponseVO findActorPage(BasePageVO basePageVO) {
        IPage<ActorResponseVo> res = filmActorsApi.findActorsPageBy(basePageVO.getNowPage(), basePageVO.getPageSize());
        Map<String, Object> result = this.buildResVo(res, "actors");
        return BaseResponseVO.success(result);
    }

    /**
     * 获取电影列表
     * @param basePageVO
     * @return
     */
    @RequestMapping
    public BaseResponseVO findFilmPage(BasePageVO basePageVO) {
        IPage<DescribeFilmsRespVO> filmPage = filmServiceApi.findFilmPage(new Page<>(basePageVO.getNowPage(), basePageVO.getPageSize()));
        Map<String, Object> resMap = this.buildResVo(filmPage, "films");
        return BaseResponseVO.success(resMap);
    }

    /**
     * 获取电影详情
     * @param filmId
     * @return
     */
    @RequestMapping(value = "/{filmId}")
    public BaseResponseVO findValidFilmDetailById(@PathVariable("filmId")String filmId) {
        DescribeFilmRespVO filmDetail = filmServiceApi.findFilmDetailById(filmId);
        return BaseResponseVO.success(filmDetail);
    }

    @RequestMapping("/test")
    public String feignTest() {
        return " hello world";
    }

    // 根据电影编号获取电影信息
    @RequestMapping(value = "/film:add",method = RequestMethod.POST)
    public BaseResponseVO saveFilmInfo(@RequestBody FilmSavedReqVO filmSavedReqVO) throws CommonServiceException {

        filmServiceApi.saveFilm(filmSavedReqVO);

        return BaseResponseVO.success();
    }

    /**
     * 构建分页返回结果
     * @param iPage
     * @param title
     * @return
     */
    private Map<String, Object> buildResVo(IPage iPage, String title){
        Map<String, Object> resMap = new HashMap<>(8);
        resMap.put("totalSize", iPage.getTotal());
        resMap.put("totalPages", iPage.getPages());
        resMap.put("pageSize", iPage.getSize());
        resMap.put("nowPage", iPage.getCurrent());
        resMap.put(title, iPage.getRecords());
        return resMap;
    }
}
